package com.swanclouds.redfish.systems;

import com.google.common.base.Preconditions;
import com.swanclouds.api.systems.MemoryService;
import com.swanclouds.core.transport.ClientConstants;
import com.swanclouds.model.common.redfish.CollectionResource;
import com.swanclouds.redfish.common.redfish.CollectionResourceModel;
import com.swanclouds.redfish.internal.BaseSystemService;
import com.swanclouds.redfish.systems.domain.MemoryModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询 内存 集合资源信息服务
 *
 * @author:  张立强
 */
public class MemoryServiceImpl extends BaseSystemService implements MemoryService {


    /**
     * 查询内存集合资源信息
     * @return CollectionResource
     */
    @Override
    public CollectionResource get() {
        return get(CollectionResourceModel.class,uri(ClientConstants.SYSTEMS_MEMORY,getSystemId())).execute();
    }

    /**
     * 查询内存集合资源实例
     *
     * @param memoryId id
     * @return Memory
     */
    @Override
    public MemoryModel getMemory(String memoryId) {
        Preconditions.checkNotNull(memoryId, "Memory Id is null");
        return get(MemoryModel.class,uri(ClientConstants.SYSTEMS_MEMORY_ID,getSystemId(),memoryId)).execute();
    }

    /**
     * 查询内存集合资源实例列表
     * @return List&lt;Memory&gt;
     */
    @Override
    public List<MemoryModel> list() {
        List<MemoryModel> memories = new ArrayList<>();
        CollectionResource resource = get();
        resource.getCollectionResourceList().stream().forEach(ref -> {
            memories.add(get(MemoryModel.class, uri(ref)).execute());
        });
        return memories;
    }
}
